<?php

namespace app\data\controller\total;

use think\admin\Controller;

/**
 * 数据报表
 * Class Portal
 * @package app\data\controller\total
 */
class Portal extends Controller
{
    /**
     * 金融数据报表
     * @auth true
     * @menu true
     */
    public function index()
    {
        /**
         *  产品数量、用户数量、投资总额、利息总额
         *
         */

        $where[] = ['deleted_status', '=', 0];
        $where[] = ['status', '<>',  0];

        $this->usersTotal   = $this->app->db->name('ShopOrder')->cache(true, 60)->group('customer_no')->count();
        $this->goodsTotal   = $this->app->db->name('ShopGoods')->cache(true, 60)->where(['deleted' => 0])->count();
        $this->amountTotal  = $this->app->db->name('ShopOrder')->cache(true, 60)->where($where)->sum('money');
        $this->lxTotal      = $this->app->db->name('ShopOrder')->cache(true, 60)->sum('expect_lxje');


        // 近十天用户及交易趋势
        $this->days = $this->app->cache->get('portals', []);
        if (empty($this->days)) {
            for ($i = 15; $i >= 0; $i--) {
                $date = date('Y-m-d', strtotime("-{$i}days"));
                $this->days[] = [
                    '当天日期' => date('m-d', strtotime("-{$i}days")),
                    '投资数量' => $this->app->db->name('ShopOrder')->whereLike('create_at', "{$date}%")->where($where)->count(),
                    '投资金额' => $this->app->db->name('ShopOrder')->whereLike('create_at', "{$date}%")->where($where)->sum('money'),
                ];
            }
            $this->app->cache->set('portals', $this->days, 60);
        }


        $end = date('Y-m');
        $begin = date('Y-m', strtotime('-12 months'));

        $this->months = $this->app->cache->get('month', []);

        if (empty($this->months)){
            while (1){
                $date = $begin = date('Y-m', strtotime("+1 month", strtotime($begin)));
                $start_time = date('Y-m-01', strtotime($begin . '-01'));
                $end_time = date('Y-m-01', strtotime('+1 month', strtotime($begin . '-01')));
                $this->months[] = [
                    '当前月份' => $date,
                    '投资数量' => $this->app->db->name('ShopOrder')->whereLike('create_at', "{$date}%")->where($where)->count(),
                    '利息金额' => $this->app->db->name('shop_order_item')->whereBetweenTime('date',$start_time, $end_time)->sum('change_amount'),
                    '投资金额' => $this->app->db->name('ShopOrder')->whereLike('create_at', "{$date}%")->where($where)->sum('money'),
                ];

                if ($begin == $end) break;
            }

        }

        $this->fetch();
    }

}